﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Padelcenter.BusinessLogic;
using log4net;
using log4net.Config;

namespace Padelcenter.Web.Admin.Menu.Productos
{
    public partial class Monedas : System.Web.UI.Page
    {
        // Instanciamos el Logger
        ILog logger = log4net.LogManager.GetLogger(typeof(Monedas));

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                // Logueamos el error en modo Debug
                logger.Debug("Page: Monedas.aspx - Method: Page_Load() - " + ex.Message.ToString());

                Response.Redirect("http://Error.aspx?");                
            }
        }

        private void BindDataGrid()
        {
            MonedaBS Mbs = null;
            try
            {
                Mbs = new MonedaBS();

                // Binding
                grdMonedas.DataSource = Mbs.MonedaSelect(null);
                grdMonedas.DataBind();
            }
            catch (Exception ex)
            {
                // Logueamos el error en modo Debug
                logger.Debug("Page: Monedas.aspx - Method: BindDataGrid() - " + ex.Message.ToString());

                // Retornamos la excepcion
                throw (new Exception("Page: Monedas.aspx - Method: BindDataGrid() " + ex.Message.ToString()));
            }
            finally
            {
                if (Mbs != null)
                    Mbs.Dispose();
                Mbs = null;
            }
        }

        protected void grdMonedas_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                grdMonedas.EditIndex = -1;
                BindDataGrid();
            }
            catch (Exception ex)
            {
              // Logueamos el error en modo Debug
              logger.Debug("Page: Monedas.aspx - Method: grdMonedas_RowCancelingEdit() - " + ex.Message.ToString());

              Response.Redirect("http://Error.aspx?");                
            }
        }

        protected void grdMonedas_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Common.Moneda oMoneda = null;
            TextBox tb = null;
            CheckBox chk = null;
            MonedaBS Mbs = null;

            try
            {
                oMoneda = new Common.Moneda();
                Mbs = new MonedaBS();
                tb = new TextBox();
                chk = new CheckBox();

                tb = (TextBox)this.grdMonedas.Rows[e.RowIndex].FindControl("txtDescripcion");
                chk = (CheckBox)this.grdMonedas.Rows[e.RowIndex].FindControl("chkEditActivo");

                oMoneda.IdMoneda = (int)this.grdMonedas.DataKeys[e.RowIndex].Value;
                oMoneda.Descripcion = tb.Text;
                oMoneda.Activo = chk.Checked;

                tb = (TextBox)this.grdMonedas.Rows[e.RowIndex].FindControl("txtCotizacion");
                oMoneda.Cotizacion = Convert.ToDouble(tb.Text.Replace(".", ","));

                tb = (TextBox)this.grdMonedas.Rows[e.RowIndex].FindControl("txtSimbolo");
                oMoneda.Simbolo = tb.Text.ToString();

                Mbs.MonedaUpdate(oMoneda.IdMoneda, oMoneda.Descripcion, oMoneda.Cotizacion, oMoneda.Simbolo, oMoneda.Activo);

                grdMonedas.EditIndex = -1;
                BindDataGrid();
            }
            catch (Exception ex)
            {
                // Logueamos el error en modo Debug
                logger.Debug("Page: Monedas.aspx - Method: grdMonedas_RowUpdating() - " + ex.Message.ToString());

                Response.Redirect("http://Error.aspx?");                            
            }
            finally
            {
                if (Mbs != null)
                    Mbs.Dispose();
                Mbs = null;

                oMoneda = null;

                if (tb != null)
                    tb.Dispose();
                tb = null;

                if (chk != null)
                    chk.Dispose();
                chk = null;
            }
        }

        protected void grdMonedas_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                grdMonedas.EditIndex = e.NewEditIndex;
                BindDataGrid();
            }
            catch (Exception ex)
            {
                // Logueamos el error en modo Debug
                logger.Debug("Page: Monedas.aspx - Method: grdMonedas_RowEditing() - " + ex.Message.ToString());

                Response.Redirect("http://Error.aspx?");
            }
        }

        protected void grdMonedas_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void grdMonedas_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grdMonedas.PageIndex = e.NewPageIndex;
                BindDataGrid();
            }
             catch (Exception ex)
             {
                 // Logueamos el error en modo Debug
                 logger.Debug("Page: Monedas.aspx - Method: grdMonedas_PageIndexChanging() - " + ex.Message.ToString());

                 Response.Redirect("http://Error.aspx?");
             }
        }
    }
}

